import {
    Theme
} from "../../model/theme";
import {
    Banner
} from "../../model/banner";
import {
    Category
} from "../../model/category";
import {
    Activety
} from "../../model/activity";

import {
    SpuPaging
} from "../../model/spu-paging";


Page({
    data: {
        grid: [],
        themeA: null,
        themeF: null,
        themeEspu: null,
        themeH: null,
        bannerB: null,
        activetyD: null,
        bannerG: null,
        paging: null, //paging实例(带有状态)
        loadingType: 'loading'
    },

    onLoad(options) {
        this.initAllData();
        this.initBottomSpuList() // 获取瀑布流数据
    },

    async initAllData() {
        // 对首页的主题集合统一查询
        const theme = new Theme();
        const themeArr = await theme.getTheme();

        const themeA = theme.getThemeA();
        const bannerB = await Banner.getHomeLocationB();
        const grid = await Category.getHomeLocationC();
        const activetyD = await Activety.getHomeLocationD();
        let themeEspu = null;
        const themeE = theme.getThemeE();
        if (themeE.online) {
            themeEspu = await Theme.getHomeLocationEspu();
        }
        const themeF = theme.getThemeF();
        const bannerG = await Banner.getHomeLocationG()
        const themeH = theme.getThemeH();
        this.setData({
            themeA,
            themeF,
            themeEspu,
            themeH,
            grid,
            bannerB,
            activetyD,
            bannerG
        });
    },

    // 获取瀑布流数据
    async initBottomSpuList() {
        //  翻页处理paging类带有状态 将其存储
        const paging = await SpuPaging.getLatestPaging()
        this.data.paging = paging
        const data = await paging.getMoreData()
        if (!data) {
            return
        }

        wx.lin.renderWaterFlow(data.items)
        if (!data.moreData) {
            //没有更多的数据了
            this.setData({
                loadingType: 'end'
            })
        }
    },

    async onReachBottom() {
        const data = await this.data.paging.getMoreData()
        if (!data) {
            this.setData({
                loadingType: 'end'
            })
            return
        }
        wx.lin.renderWaterFlow(data.items)
    },

    onShareAppMessage: function() {}
});